﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class Admin_Country : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTopMsg.Text = "";
        Page.Title = ConfigurationManager.AppSettings["AppOwner"] + " " + ConfigurationManager.AppSettings["AppShortName"] + " :: Country Management";
        lblHeading1.Text = ConfigurationManager.AppSettings["AppOwner"];
        lblHeading2.Text = ConfigurationManager.AppSettings["AppName"];

    }

    protected void gvCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        //winEdit.Hide = false;
    }

    protected void frmAdd_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        //if (e.CommandName.Equals("Cancel"))
            //winAdd.Hide = true;
    }

    /// <summary>
    /// Reload Site Map after the Categories are updated
    /// </summary>
    protected void frmAdd_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        //winAdd.Hide = true;
    }

    protected void odsUser_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {
        // Get new product Id
        bool status = (bool)e.ReturnValue;
        if (status == true)
        {
            lblTopMsg.Text = "New User Added Succesfully";
            lblTopMsg.Visible = true;

            //remove any search criteria which may prevent this record from 
            //being shown in the grid
            //txtSearchUsername.Text = "";
            //txtSearchName.Text = "";

            //Set gridview pageindex to display the last page(assuming newly added record is displayed on the last page)

            gvCountry.PageIndex = Int32.MaxValue;
        }
        else
        {
            lblTopMsg.Text = "Unable to add new User. Please try again !";
            lblTopMsg.Visible = true;
        }
    }

    protected void odsEditUser_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
    {
        lblTopMsg.Text = "User record deleted succesfully";
        lblTopMsg.Visible = true;
        gvCountry.DataBind();
    }

    protected void odsEditUser_Updated(object sender, ObjectDataSourceStatusEventArgs e)
    {
        // Get new product Id
        bool status = (bool)e.ReturnValue;
        if (status == true)
        {
            lblTopMsg.Text = "User details updated succesfully";
            lblTopMsg.Visible = true;
            gvCountry.DataBind();
        }
        else
        {
            lblTopMsg.Text = "Unable to update User details. Please try again !";
            lblTopMsg.Visible = true;
        }
    }

    protected void frmEdit_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        //winEdit.Hide = true;
    }

    protected void gvCountry_PageIndexChanged(object sender, EventArgs e)
    {
        gvCountry.DataBind();
    }
    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        //get the gridviewrow from the sender so we can get the datakey we need
        LinkButton btnDetails = sender as LinkButton;
        GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
        string UserId = gvCountry.DataKeys[row.RowIndex].Values[0].ToString();

        //  Extract the AgencyCode from the row whose edit button originated the postback.
        //  grab the AgencyCode and feed it to the Agency details datasource
        //  finally, rebind the GridView & FormView
        //this.odsEditUser.SelectParameters.Clear();
        //this.odsEditUser.SelectParameters.Add("user_id", UserId);
        this.odsCountry.DataBind();
        //----this.frmEdit.DataBind();
        //Show the Edit Modal window
        this.mdlPopup.Show();
    }

    //This method will be called from client side using Ajax
    [System.Web.Services.WebMethod]
    public static string ValidateWhileInserting(string userName)
    {
        string duplicateRecord = "N";
        ManageUser objUser = new ManageUser();
        if (objUser.IsUserNameDuplicate(userName))
            duplicateRecord = "Y";
        return duplicateRecord;
    }

    //This method will be called from client side using Ajax
    [System.Web.Services.WebMethod]
    public static string ValidateWhileUpdating(string userName, string userId)
    {
        string duplicateRecord = "N";
        ManageUser objUser = new ManageUser();
        if (objUser.IsUserNameDuplicate(userName, userId))
            duplicateRecord = "Y";
        return duplicateRecord;
    }

    protected void cvCheckUserName_ServerValidate(object source, ServerValidateEventArgs args)
    {
        CustomValidator cv = (CustomValidator)source;
        ManageUser objUser = new ManageUser();
        if (cv.ValidationGroup.Equals("Add"))
        {
            if (objUser.IsUserNameDuplicate(args.Value))
                args.IsValid = false;
            else
                args.IsValid = true;
        }
        else if (cv.ValidationGroup.Equals("Edit"))
        {
            HiddenField hdnUserId = (HiddenField)cv.Parent.FindControl("hdnUserId");
            if (hdnUserId != null && hdnUserId.Value.Trim().Length > 0)
            {
                if (objUser.IsUserNameDuplicate(args.Value, hdnUserId.Value.Trim()))
                    args.IsValid = false;
                else
                    args.IsValid = true;
            }
        }


    }
}